package view;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import controler.Listener;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JScrollPane;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.DefaultComboBoxModel;

import entities.ExchangeRate;

public class SwingView implements View
{

	private JFrame				frame;
	private DefaultListModel	listModel						= new DefaultListModel();
	private List<Listener>		getExchangeRateListListeners	= new ArrayList<Listener>();
	private JButton btnNewButton;
	private JComboBox comboBox;
	private JLabel lblTypListy;

	/**
	 * Create the application.
	 */
	public SwingView()
	{
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize()
	{
		frame = new JFrame();
		frame.setResizable(false);
		frame.setBounds(100, 100, 600, 600);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(0, 45, 594, 527);
		frame.getContentPane().add(scrollPane);
		
		JList list = new JList(listModel);
		scrollPane.setViewportView(list);
		
		btnNewButton = new JButton("Poka\u017C kursy");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				listModel.clear();
				notifyGetExchangeRateListListeners();
			}
		});
		btnNewButton.setBounds(10, 11, 140, 23);
		frame.getContentPane().add(btnNewButton);
		
		comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"a", "b", "c", "h"}));
		comboBox.setBounds(247, 14, 150, 20);
		frame.getContentPane().add(comboBox);
		
		lblTypListy = new JLabel("typ listy:");
		lblTypListy.setBounds(189, 20, 64, 14);
		frame.getContentPane().add(lblTypListy);
	}
	
	private void notifyGetExchangeRateListListeners ()
	{
		for (Listener listener : getExchangeRateListListeners)
		{
			listener.inform((String)comboBox.getSelectedItem());
		}
	}

	@Override
	public void start()
	{
		frame.setVisible(true);
	}

	@Override
	public void showMessage(String message)
	{
		JOptionPane.showMessageDialog(frame, message);
	}

	@Override
	public void showExchangeRateList(Map<String, ExchangeRate> exchange_rate_map)
	{
		for(Entry<String, ExchangeRate> entry : exchange_rate_map.entrySet())
		{
			listModel.addElement(entry.getValue().toString());
		}
	}

	@Override
	public void addGetExchangeRateListListener(Listener listener)
	{
		getExchangeRateListListeners.add(listener);
	}

	@Override
	public void removeGetExchangeRateListListener(Listener listener)
	{
		getExchangeRateListListeners.remove(listener);
	}
}
